home *** CD-ROM | disk | FTP | other *** search
/ L' Effet Pommier 3 / L'Effet Pommier - Volume 03.iso / Programmation / gray image 2.1 / mymenv.cc < prev    next >
Text File  |  1995-06-13  |  5KB  |  167 lines

  1. // This may look like C code, but it is really -*- C++ -*-
  2. /*
  3.  ************************************************************************
  4.  *                      Macintosh Service C++ functions
  5.  *           that support the standard environment for me on Macintosh
  6.  *
  7.  * $Id: myenv.cc,v 1.3 1995/02/08 17:23:50 oleg Exp oleg $
  8.  */
  9.  
  10.  
  11. #include "mymenv.h"
  12. #include <stdarg.h>
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <string.h>
  16. #include <Sound.h>
  17. #include <new.h>
  18.  
  19.  
  20.  
  21. static const int System_Alert_rsc = -16411;            // ID of the System ALRT 
  22.                                                     // resource (in the System file)
  23. /*
  24.  *------------------------------------------------------------------------
  25.  *            Something got screwed up and we're out of here
  26.  * Synopsis
  27.  *    volatile void _die(const char * message,... );
  28.  *    Message may contain format control sequences %x. Items to print 
  29.  *    with the control sequences are to be passed as additional arguments to
  30.  *    the function call.
  31.  */
  32.  
  33. volatile void _die(const char * message,...)
  34. {
  35.   va_list args;
  36.   char buffer[1000];
  37.   va_start(args,message);            // Init 'args' to the beginning of
  38.                                     // the variable length list of args
  39.   vsprintf(buffer,message,args);
  40.   strncat(buffer,"\rSorry man, enough is enough, I'm outta here",sizeof(buffer)-strlen(buffer)-1);
  41.   CtoPstr(buffer);                            // Convert to Pascal-like string
  42.   ParamText((ConstStr255Param)buffer,nil,nil,nil); // Set the string in the Alert resource
  43.   Alert(System_Alert_rsc,0L);                // Display the Alert box and the string
  44.                                               // set just before
  45.   exit(4);
  46. }
  47.  
  48.                                     // This guy handles the situation when the memory
  49.                                     // allocation through 'new' has failed
  50. static void my_new_failed_handler(void)
  51. {
  52.     _die("Flat out of memory");
  53. }
  54.  
  55. /*
  56.  *------------------------------------------------------------------------
  57.  *                    Just tell the user something
  58.  * Synopsis
  59.  *    void alert(const char * text,... );
  60.  *    Message may contain format control sequences %x. Items to print 
  61.  *    with the control sequences are to be passed as additional arguments to
  62.  *    the function call.
  63.  */
  64.  
  65. void alert(const char * text,...)
  66. {
  67.   va_list args;
  68.   char buffer[1000];
  69.   va_start(args,text);                        // Init 'args' to the beginning of
  70.                                             // the variable length list of args
  71.   vsprintf(buffer,text,args);
  72.   CtoPstr(buffer);                            // Convert to Pascal-like string
  73.   ParamText((ConstStr255Param)buffer,nil,nil,nil); // Set the string in the Alert resource
  74.   Alert(System_Alert_rsc,0L);                // Display the Alert box and the string
  75. }
  76.  
  77. //------------------------------------------------------------------------
  78. //                  Patches to the standard environment
  79.  
  80. #ifndef _myenv_h
  81.                                 // Like strncpy(), but ALWAYS terminates
  82.                                 // the destination string
  83. char * xstrncpy(char * dest, const char * src, const int len)
  84. {
  85.   strncpy(dest,src,len);
  86.   dest[len] = '\0';
  87.   return dest;
  88. }
  89.  
  90. #endif
  91.  
  92.                                     // Used to "implicitly" convert from a C to Pascal
  93.                                     // string
  94. Pstr::Pstr(const char * c_str)
  95. {
  96.     strncpy((char *)pas_string,c_str,sizeof(*this)-2);
  97.     CtoPstr((char *)pas_string);
  98. }
  99.  
  100. //------------------------------------------------------------------------
  101. //                            Sound playing on errors
  102.  
  103.  
  104. static Handle error_sound_handle = nil;
  105.  
  106. static pascal void error_sound_player(short sound_no)
  107. {
  108.   if( error_sound_handle != nil )
  109.     SndPlay(nil,(SndListHandle)error_sound_handle,FALSE);
  110.   else
  111.     SysBeep(10);
  112.   error_sound_handle = nil;
  113. }
  114.  
  115. void set_error_sound(Str255 sound_name)
  116. {
  117.   error_sound_handle = GetNamedResource('snd ',sound_name);
  118.   if( error_sound_handle != nil )
  119.     ErrorSound(NewSoundProc(error_sound_player));
  120. }
  121.  
  122.  
  123. /*
  124.  *------------------------------------------------------------------------
  125.  *               Suspending the application for a specified period of time
  126.  *
  127.  * Unlike Delay(), the present function is generous in that in relinquishes the
  128.  * CPU control for a specified time and thus lets other (backgrounded) application
  129.  * run.
  130.  * The generous waiting is implemented as waiting for a null event which is to
  131.  * be sent to the application after the specifed timed interval expired. Note
  132.  * the application can get woken up before the time comes if other application
  133.  * calls WakeUpProcess().
  134.  */
  135.  
  136. void sleep(const int nticks)
  137. {
  138.     EventRecord event;
  139.     const int event_mask = 0;                // Wait for null events only
  140.     WaitNextEvent(event_mask,&event,nticks,nil);
  141. }
  142.  
  143.  
  144. /*
  145.  *------------------------------------------------------------------------
  146.  *                            Initialize the Macintosh
  147.  *                 Initialize all the managers & memory
  148.  */
  149.  
  150. void Initialize_MAC(void)
  151. {
  152.     MaxApplZone();
  153.     
  154.     InitGraf(&qd.thePort);
  155.     InitFonts();
  156.     FlushEvents(everyEvent, 0);
  157.     InitWindows();
  158.     InitMenus();
  159.     TEInit();
  160.     InitDialogs(0);
  161.     InitCursor();
  162.     
  163.     set_new_handler(my_new_failed_handler);
  164. }
  165.  
  166.  
  167.